import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:magic_car/UI/Loading.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:magic_car/util/colors.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:toast/toast.dart';

class ApplyTestPage extends StatefulWidget {
  //final String oid;
  ApplyTestPage();

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<ApplyTestPage> {
  List formList;
  //final String oid;
  _MyHomePageState();
  TextEditingController _content = new TextEditingController();
  FocusNode _contentFocusNode = FocusNode();
  initState() {
    super.initState();
    
    loaddata();
  }

  //加载数据
  loaddata() async {
    Map<String, String> map = new Map();
   // map['oid'] = this.oid;
    //  SharedPreferences pref = await SharedPreferences.getInstance();
    //  map["id"]=pref.getString('uid');
    //  map["type"]="0";

    HttpHelper.post(HttpHelper.API_OrderDetail, (data) async {
      if (data != null) {
        if (data["code"].toString() == "1") {
          List oldlist = formList;
          var orderinfo = data["info"];
          for (var i = 0; i < oldlist.length; i++) {
            var item = oldlist[i];
            var str = "";
            if (i == 0)
              str = orderinfo["name"];
            else if (i == 1)
              str = orderinfo["phone_num"];
            else if (i == 2)
              str = orderinfo["name"];
            else if (i == 3)
              str = orderinfo["phone_num"];
            else if (i == 4)
              str = orderinfo["add_time"];
            else
              str = orderinfo["address"];

            item["sub_title"] = str;
          }

          setState(() {
            // formList = oldlist;
            // orders=data["info"];
          });
          //pref.setStringList('userinfo', data["info"]);

        }
      }
    }, params: map);
  }

  Widget buildcontent() {
    List<Widget> tiles = [];
    Widget content;

    tiles.add(Padding(
        padding: new EdgeInsets.all(10),
        child: Container(
            height: 230,
            decoration: new BoxDecoration(
//背景
              color: Colors.white,
              //设置四周圆角 角度
              borderRadius: BorderRadius.all(Radius.circular(4.0)),
              //设置四周边框
            ),
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
              new Padding(padding:new EdgeInsets.fromLTRB(10, 10, 10, 10),child:
              new Text("申请原因：")),
              new Container(
                  height: 160,
                  child: 
                  
                  new TextField(
                    focusNode: _contentFocusNode,
                    controller: _content,
                    
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      contentPadding: EdgeInsets.all(10)
                    ),
                    maxLines: 10,
                  ))
            ]))));
            tiles.add(
              new Padding(
                padding: new EdgeInsets.all(20),
                child: SizedBox(
                      width: double.infinity,
                      height: 45,
                      child: new RaisedButton.icon(
                        icon:
                            new Image.asset('resource/images/user/answer.png'),
                        label: Text(
                          '提交申请',
                          style: TextStyle(fontSize: 15),
                        ),
                        color: Colours.app_main,
                        textColor: Colors.white,
                        onPressed: (){
                          //提交
                          _contentFocusNode.unfocus();
                          submit();

                        },
                      )),
              )
            );

    content = new Column(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }
  //提交答案
 submit() async{
  if(_content.text.trim().length==0){
    Toast.show('请输入补考原因', context);
    return;
  }
  // LoadingPage loadpage = new LoadingPage(context);
  // loadpage.show();
  var content = _content.text.trim();
  //提交回答内容
    Map<String, String> map = new Map();
   // map['oid'] = this.oid;
     SharedPreferences pref = await SharedPreferences.getInstance();
     map["id"]=pref.getString('uid');
     map["reason"]=content;
    //  map["type"]="0";

    HttpHelper.post(HttpHelper.API_ApplyTest, (data) async {
     // loadpage.close();
      if (data != null) {
        Navigator.pop(context);
       Toast.show(data["msg"], context);
       
      }
    }, params: map);
 }
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(
            '申请补考',
            style: TextStyle(color: Colors.black),
          ),
          backgroundColor: Colors.white,
          iconTheme: new IconThemeData(color: Colors.black),
          elevation: 0,
        ),
        body: new Center(
            child: Container(
          decoration: new BoxDecoration(
            color: Color.fromRGBO(238, 238, 238, 1),
          ),
          child: Padding(
              padding: new EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: new ListView(
                children: <Widget>[buildcontent()],
              )),
        )));
  }
}
